﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using HtmlAgilityPack;
using Newtonsoft.Json;

namespace ExtjsDocsParser
{
    class Program
    {
        static void Main(string[] args)
        {
            const string jsFilesMask = "C:\\Temp\\ExtJs\\";
            var classes = new Dictionary<string, ExtClassDoc>();
            foreach (var file in Directory.GetFiles(jsFilesMask, "ext*.js"))
            {
                if (file.Contains("Ext.panel.Panel.js"))
                {
                    var model = ParseFile(System.IO.File.ReadAllText(file));
                    classes.Add(model.Name, model);
                    Console.WriteLine("Parsed " + model.Name);
                }
            }

            const string outputDir = "C:\\Temp\\Out\\";
            var panel = classes["Ext.panel.Panel"];
            ExportPanel(panel, outputDir);

        }

        private static void ExportPanel(ExtClassDoc panel, string outputDir)
        {
            var sb = new SharpBuilder();
            sb.StartInterface("IPanelFactory");
            sb.StartMethod("PanelPanel", "IPanelPanel");
            foreach (var config in panel.Members.Cfg)
            {
                sb.AddParam(config.Name);
            }
            sb.EndMethod();
            sb.EndInterface();

            string s = sb.Result();
        }

        private static ExtClassDoc ParseFile(string file)
        {
            // skip Ext.data.JsonP.Ext_Component(
            //var jsonStarts = file.IndexOf("(") + 1;
            // truncate );
            //var json = file.Substring(jsonStarts, file.Length - jsonStarts - 2);


            var classModel = JsonConvert.DeserializeObject<ExtClassDoc>(file);
            var doc = new HtmlDocument();
            HtmlReader.ParseDocs(doc, classModel);
            return classModel;
            Debug.Assert(classModel.TagName == "class");
        }

    }
}
